<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../css/common.css" media="all">

	</head>
	<body>

		<div class="layui-fluid">
			<div class="layui-row" id="view">
				<form class="layui-form" lay-filter="test_detail">
					<div class="layui-card">
						<div class="layui-card-body">
							<div class="layui-form-item">
								<label class="layui-form-label">状态</label>
								<div class="layui-input-block on_off"></div>
							</div>

							<!--<div class="layui-form-item">
								<label class="layui-form-label">选择分类</label>
								<div class="layui-input-block">
									<select class="catid" name="fid"></select>
								</div>
							</div>-->
							<div class="layui-form-item">
								<div class="layui-form-inline">
									<label class="layui-form-label">分组：</label>  
								</div>
								<div class="layui-input-block" id="groupid">
								 	<select lay-search="" lay-filter="groupid" lay-verify="" name="groupid" class="site_icon_group"></select>
								</div>
							</div>

							<div class="layui-form-item">
								<label class="layui-form-label">文章标题</label>
								<div class="layui-input-block">
									<input type="text" name="title" lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">缩略图</label>
								<div class="layui-input-block">
									<button type="button" class="layui-btn layui-btn-normal" onclick="choose(1)">上传文件</button>
									<ul class="show_img_ul" id="showimg"></ul>
									<!-- <textarea placeholder="请输入售后保障" name="support" class="layui-textarea"></textarea> -->
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">文件</label>
								<div class="layui-input-block">
									<button type="button" class="layui-btn layui-btn-normal" onclick="choose(2)">上传文件</button>
									<ul class="show_img_ul" id="showfile"></ul>
									<!-- <textarea placeholder="请输入售后保障" name="support" class="layui-textarea"></textarea> -->
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">来源链接</label>
								<div class="layui-input-block">
									<input type="text" name="resource_url1" placeholder="来源链接" autocomplete="off" class="layui-input">
								</div>
							</div>
							
							<div class="layui-form-item">
								<label class="layui-form-label">文章内容</label>
								<div class="layui-input-block">
									<textarea class="layui-textarea layui-hide" name="content" lay-verify="content" id="demo_editor"></textarea>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">内容描述</label>
								<div class="layui-input-block">
									<textarea placeholder="请输入内容描述" name="description" class="layui-textarea"></textarea>
								</div>
							</div>
							<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">作者</label>
									<div class="layui-input-inline">
										<input type="text" name="author" placeholder="请输入作者" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-inline">
									<label class="layui-form-label">来源</label>
									<div class="layui-input-inline">
										<input type="text" name="resource" placeholder="请输入来源" autocomplete="off" class="layui-input">
									</div>
								</div>
							</div>

						</div>
					</div>

					<div class="layui-form-item layui-layout-admin">
						<div class="layui-input-block">
							<div class="layui-footer" style="left: 0;">
								<!-- <button type="reset" class="layui-btn layui-btn-primary">重置</button> -->
								<button class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
		<script src="../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../js/jquery.min.js"></script>
		<script src="../../../js/jquery.jqprint-0.3.js"></script>
		<script src="../../../js/ace/ace.js"></script>
		<script src="../../../js/common.js"></script>
		<script>
			var _data = {
				id: getParam('id')
			}
			// console.log(_data.id);
			layui.config({
				base: '../../../layuiadmin/' //静态资源所在路径
			}).extend({
				tinymce: '../tinymce/tinymce',
				index: 'lib/index' //主入口模块
			}).use(['index', 'form', 'laytpl', 'laydate', 'layedit','tinymce','util'], function() {
				var $ = layui.$,
					admin = layui.admin,
					layedit = layui.layedit,
					laytpl = layui.laytpl,
					laydate = layui.laydate,
					tinymce = layui.tinymce,
					util = layui.util,
					form = layui.form;

				var maplist = JSON.parse(localStorage.getItem('lsmap')),
					img_arr1 = [];
					img_arr2 = [];

				var edit = tinymce.render({
					elem: "#demo_editor",
					height: 600
				});
				
				//inithelp_catshow('option');
				site_icon_group_show("option",{type:2}) 
				on_off_show('radio')
				
				
				if(_data.id){
					setTimeout(function(){
						loading()
						initdata();
					},200)
				} 
				
				util.event('lay-event', {
					preveiw: function() {
						layer.open({
							type: 2,
							title: '预览',
							shadeClose: false,
							shade: 0.5,
							maxmin: true, //开启最大化最小化按钮
							area: ['60%', '80%'],
							content: 'pre_content.html',
							end: function() {},
							success: function(layero, index) {
								var body = layer.getChildFrame('body', index);
								body.find(".title").html($('input[name=title]').val());
								body.find(".showcontent").html(edit.getContent());
							}
						});
					},
				});
				

				form.on('radio(choosetype)', function(data) {
					console.log(data);
				});

				//监听提交
				form.on('submit(demo1)', function(data) {
					var data = data.field;
					_data = Object.assign(_data, data);
					_data.thumbnail = img_arr1.join(',');
					_data.resource_url = data.resource_url1 ? data.resource_url1 : img_arr2.join(',');
					_data.content = edit.getContent()
					_data.groupname = $("select[name=groupid] option:selected").text()
					layer.confirm('确定提交数据？', function() {
						loading();
						if(!_data.id){
							delete _data.id
							submitdata(server.addsite_help);
						} else {
							submitdata(server.updatesite_help);
						}
					}, function() {
						layer.msg('取消操作');
					});
					return false;
				});

				function submitdata(_url) {
					console.log(_data,'_data2222')
					$.post(_url, _data, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								layer.alert('操作成功', function() {
									parent.layer.closeAll();
									parent.layui.table.reload('test-table-operate');
								});
							} else {
								layer.msg(res.msg);
								layer.closeAll('loading');
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

				window.choose = function(data) {
					layer.open({
						type: 2,
						title: '上传文件',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['700px', '400px'],
						content: '../../model/up_report_one.html?type=' + data,
						end: function() {},
						success: function() {}
					});
				}

				window.getimg = function(data, type) {
						console.log(data);
					if (data) {
						switch (type) {
							case '1':
								createli(data, 'showimg', type)
								break;
							case '2':
								createli(data, 'showfile', type)
								break;
							default:
								break;
						}
					}
				}
				window.delimg = function(i, type, _self) {
					var _type = String(type)
					layer.confirm('确定要删除图片？', function(index) {
						switch (_type) {
							case '1':
								img_arr1.splice(i, 1)
								break;
							case '2':
								img_arr2.splice(i, 1)
								break;
							default:
								break;
						}
						$(_self).parent().remove();
						layer.close(index)
					}, function() {
						layer.msg('取消操作');
					});
					// console.log(i);
					// console.log(type);

				}

				function createli(data, dom, type) {
					// console.log(data);
					var lihtml = '';
					switch (type) {
						case '1':
							if (data.indexOf(',') == -1) {
								img_arr1.splice(0, 1, data)
							} else {
								for (var i = 0; i < data.split(',').length; i++) {
									img_arr1.push(data.split(',')[i])
								}
							}
							for (var i = 0; i < img_arr1.length; i++) {
								console.log(img_arr1[i]);
								if(img_arr1[i].indexOf('.png') > -1 || img_arr1[i].indexOf('.jpg') > -1 || img_arr1[i].indexOf('.jpeg') > -1){
									lihtml += '<li class="show_img_li">' +
										'<img src=' + img_arr1[i] + ' >' +
										'<p onclick="delimg(' + i + ',1,this)">删除</p>' +
										'</li>'
								} else {
									lihtml += '<li class="show_img_li">' +
										'<span class="layui-icon layui-icon-file-b">' +
										'<p onclick="delimg(' + i + ',1,this)">删除</p>' +
										'</li>'
								}
							}
							break;
						case '2':
							if (data.indexOf(',') == -1) {
								img_arr2.splice(0, 1, data)
							} else {
								for (var i = 0; i < data.split(',').length; i++) {
									img_arr2.push(data.split(',')[i])
								}
							}
							for (var i = 0; i < img_arr2.length; i++) {
								if(img_arr2[i].indexOf('.png') > -1 || img_arr2[i].indexOf('.jpg') > -1 || img_arr2[i].indexOf('.jpeg') > -1){
									lihtml += '<li class="show_img_li">' +
										'<img src=' + img_arr2[i] + ' >' +
										'<p onclick="delimg(' + i + ',1,this)">删除</p>' +
										'</li>'
								} else {
									lihtml += '<li class="show_img_li">' +
										'<span class="layui-icon layui-icon-file-b">' +
										'<p onclick="delimg(' + i + ',1,this)">删除</p>' +
										'</li>'
								}
							}
							break;
						default:
							break;
					}
					$('#' + dom).html(lihtml);
				}

				function initdata() {
					$.get(server.querysite_helpdetail, _data, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								var data = res.news_detail 
								
								form.val("test_detail", data.data);
								form.render();
								
								data.data.thumbnail ? createli(data.data.thumbnail, 'showimg', '1') : ''
								data.data.resource_url ? createli(data.data.resource_url, 'showfile', '2') : ''
								// layedit.setContent(index,data.data.content)
								edit.setContent(data.data.content) 
								closeloading()
							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

			});
		</script>
	</body>
</html>
